package connect_to_database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class connection {

    //查询数据库
    void Check(){
        PreparedStatement ps = null;
        Connection connection = JDBCUtil.getConnection();
        ResultSet rs = null;
        String sql="select * from cat";
        try{
            ps=connection.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                System.out.print("ID:"+rs.getInt("ID"));
                System.out.print("\t");
                System.out.print("name:"+rs.getString("name"));
                System.out.print("\t");
                System.out.print("weight:"+rs.getDouble("weight"));
                System.out.print("\t");
                System.out.print("birthday:"+rs.getDate("birthday"));
                System.out.println();
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }finally {
            try{
                rs.close();
            }catch (SQLException throwables){
                throwables.printStackTrace();
            }
            try{
                ps.close();
            }catch (SQLException throwables){
                throwables.printStackTrace();
            }
            JDBCUtil.release(connection);
        }
    }
    //修改数据库
    void Update(int id,String name,double weight,String year){
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement ps=null;
        String sql="update cat set name=?,weight=?,birthday=? WHERE ID=?";

        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(4,id);
            ps.setString(1,name);
            ps.setDouble(2,weight);
            ps.setString(3,year);
            Integer i=ps.executeUpdate();
            System.out.println("当前受影响的记录数："+i);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            JDBCUtil.release(connection);
        }
    }
    //删除表中数据
    void Delete(int id) {
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement ps = null;
        String sql = "DELETE FROM cat WHERE ID=?";

        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            Integer i = ps.executeUpdate();
            System.out.println("当前受影响的记录数：" + i);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            JDBCUtil.release(connection);
        }
    }
    //增加表中数据
    void Inset(int id,String name,double weight,String birthday){
        Connection connection = JDBCUtil.getConnection();
        PreparedStatement ps=null;
        String sql="INSERT INTO cat VALUES(?,?,?,?)";

        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(1,id);
            ps.setString(2,name);
            ps.setDouble(3,weight);
            ps.setString(4,birthday);
            Integer i=ps.executeUpdate();
            System.out.println("当前受影响的记录数："+i);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            JDBCUtil.release(connection);
        }

    }
}
